<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring MVC 计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }

        .calculator {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .input-group {
            margin-bottom: 15px;
        }

        .input-group label {
            display: block;
            margin-bottom: 5px;
        }

        .input-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        #result {
            margin-top: 20px;
            text-align: center;
            font-weight: bold;
        }
    </style>
</head>

<body>
<div class="calculator">
    <div class="input-group">
        <label for="num1">第一个数字:</label>
        <input type="number" id="num1" required>
    </div>
    <div class="input-group">
        <label for="num2">第二个数字:</label>
        <input type="number" id="num2" required>
    </div>
    <div class="input-group">
        <button onclick="calculate('add')">加法</button>
    </div>
    <div class="input-group">
        <button onclick="calculate('subtract')">减法</button>
    </div>
    <div class="input-group">
        <button onclick="calculate('multiply')">乘法</button>
    </div>
    <div class="input-group">
        <button onclick="calculate('divide')">除法</button>
    </div>
    <div id="result"></div>
</div>

<script>
    function calculate(operation) {
        const num1 = parseFloat(document.getElementById('num1').value);
        const num2 = parseFloat(document.getElementById('num2').value);

        if (isNaN(num1) || isNaN(num2)) {
            document.getElementById('result').innerHTML = '请输入有效的数字';
            return;
        }

        const xhr = new XMLHttpRequest();
        xhr.open('GET', `/${operation}?num1=${num1}&num2=${num2}`, true);

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    document.getElementById('result').innerHTML = `结果: ${xhr.responseText}`;
                } else {
                    document.getElementById('result').innerHTML = `错误: ${xhr.responseText}`;
                }
            }
        };

        xhr.send();
    }
</script>
</body>

</html>